package org.databasefacade.data;


import org.databasefacade.table.column.ColumnMetaData;
import org.databasefacade.table.column.datatype.DataType;


public class ColumnDataValue implements DataValue
{
	private Object columnValue=null;
	private String stringColumnValue=null;
	private ColumnMetaData columnMetaData=null;
	private RenderingMetaData renderingMetaData=null;
	
	public ColumnDataValue(ColumnMetaData meta) {
		columnMetaData = meta.getCopy();
	}
	
	public ColumnDataValue(ColumnMetaData meta, RenderingMetaData render) {
		columnMetaData = meta.getCopy();
		renderingMetaData = render.getCopy();
	}
	
	
	public void setTableName(String tableName) {
		columnMetaData.setTableName(tableName);
	}
	
	public String getTableName() {
		return columnMetaData.getTableName();
	}
	
	public void setColumnName(String colName) {
		columnMetaData.setColumnName(colName);
	}
	
	public String getColumnName() {
		return columnMetaData.getColumnName();
	}
	
	
	public void setDataType(DataType dataType) {
		columnMetaData.setDataType(dataType);
	}
	
	public DataType getDataType() {
		return columnMetaData.getDataType();
	}
	
	public void setRenderingMetaData(RenderingMetaData rmd) {
		renderingMetaData = rmd.getCopy();
	}
	
	public RenderingMetaData getRenderingMetaData() {
		return renderingMetaData;
	}
	
	public void setDataValue(Object data)
	{
		columnValue = columnMetaData.getDataType().getValueInstance(data);
		setDataStringValue(data);
	}
   
	public Object getDataValue()
	{
		return columnValue;
	}
	   
	
	// TBD - We are going to have to revisit an deal with Real and Floats correctly
	public void setDataStringValue(Object value) {
		
		// Set value to null if the parameter is null
		if ( value == null ) {
			stringColumnValue = null;
			return;
		}
		
		// Format the value based on type
    	short columnType = columnMetaData.getColumnType();
    	String formattedText = value.toString();
        if (columnType == DataType.DATE_TYPE) {
            formattedText = value.toString().replaceAll("-", "");
        } else if (columnType == DataType.MONEY_TYPE) {
        	formattedText = formatDecimal(2, value.toString());
        } else if (columnType == DataType.FLOAT_TYPE) {
        	formattedText = formatDecimal(6, value.toString());
        } 
        stringColumnValue = formattedText;
	}
	
	public String getDataStringValue()
	{
		return stringColumnValue;
	}
	
	public DataValue getCopy(DataValue rec) {
		return getCopy(rec, false);
	}
	
	public DataValue getCopyWithClearedValues(DataValue rec) {
		return getCopy(rec, true);
	}
	
	private DataValue getCopy(DataValue rec, boolean useClearValues) {
		return rec;
	}
    
	
    
    /**
     * Utility methods to format the data value
     */
    private String formatDecimal(int len, String formattedText) {
        if (formattedText == null) {
            return null;
        }
        if (formattedText.contains("e")) {
            formattedText = formattedText.substring(0,
                    formattedText.indexOf("e"));
        }
        if (formattedText.contains("E")) {
            formattedText = formattedText.substring(0,
                    formattedText.indexOf("E"));
        }
        if (formattedText.contains(".")) {
            int dot = formattedText.indexOf(".");
            if (formattedText.length() > (dot + len + 1)) {
                return formattedText.substring(0, (dot + len + 1));
            }
        }
        return formattedText;
    }   
}
